﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Public Class ObjectMother

    Public Shared Function CreateMetaDataDocument() As XDocument

        Dim el As XElement = _
            <Database Name="MyDb" Class="MyDbClass" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007"> _
                <Table Name="dbo.Table1Name" Member="Table1Member">
                    <Type Name="Type1Name">
                        <Column Name="Column1Name" Member="Column1Member"/>
                        <Column Name="Column2Name" Member="Column2Member"/>
                        <Column Name="ColumnWithNoMemberName"/>
                        <Association Name="Association1Name" Member="Association1Member" OtherKey="Column1Member" ThisKey="Column2Member"/>
                        <Association Name="Association2Name" Member="Association2Member" OtherKey="Column2Member" ThisKey="Column1Member"/>
                        <Association Name="NoOtherKey" ThisKey="Column1Member" Member="AssociationNo1"/>
                        <Association Name="NoThisKey" OtherKey="Column1Member" Member="AssociationNo2"/>
                        <Association Name="MissingMember" OtherKey="Column1Member"/>
                    </Type>
                </Table>
                <Table Name="dbo.TableWithNoTypeName" Member="TableWithNoTypeName">
                    <Type>
                        <Column Name="Column1Name" Member="Column1Member"/>
                        <Column Name="ColumnWithNoMemberName"/>
                        <Association Member="AssocWithRef" Name="AssocWithRef" Type="NormalType" ThisKey="Column1Name" OtherKey="ColumnWithNoMemberName"/>
                    </Type>
                </Table>
                <Table Name="dbo.NormalTable" Member="TableWithNoTypeName">
                    <Type Name="NormalType">
                        <Column Name="Column1Name" Member="Column1Member"/>
                        <Column Name="ColumnWithNoMemberName"/>
                    </Type>
                </Table>
                <Table Name="dbo.TableWithNoMember">
                    <Type Name="NormalType">
                        <Column Name="Column1Name" Member="Column1Member"/>
                        <Column Name="ColumnWithNoMemberName"/>
                    </Type>
                </Table>
            </Database>
        Dim doc As New XDocument(New XDeclaration("1.0", "UTF-16", "yes"), el)
        Return doc

    End Function

    Public Shared Function CreateMetaDataDocument(ByVal numberOfColumns As Integer, ByVal numberOfAssociations As Integer) As XmlMetaData
        Dim cols As New List(Of XElement)
        Dim assocs As New List(Of XElement)

        Dim dbName As XName = Names.DatabaseXName
        Dim tableName As XName = Names.TableXName
        Dim colName As XName = Names.ColumnXName
        Dim assocName As XName = Names.AssociationXName
        Dim typeName As XName = Names.TypeXName

        For i As Integer = 0 To (numberOfColumns - 1)
            cols.Add(New XElement(colName, New XAttribute("Name", "Column" & i.ToString())))
        Next

        For i As Integer = 0 To (numberOfAssociations - 1)
            cols.Add(New XElement(assocName, New XAttribute("Name", "Association" & i.ToString())))
        Next

        Dim doc As New XDocument(New XDeclaration("1.0", "UTF-16", "yes"), _
                                 New XElement(dbName, New XAttribute("Name", "MyDb"), _
                                              New XElement(tableName, New XAttribute("Name", "Table1"), _
                                                           New XElement(typeName, New XAttribute("Name", "Type1"), cols.ToArray(), assocs.ToArray) _
                                                           ) _
                                                           ) _
                                                           )
        Return New XmlMetaData(doc)
    End Function

    Public Shared Function CreateMetaDataDocument(ByVal numberOfColumns As Integer) As XmlMetaData
        Return CreateMetaDataDocument(numberOfColumns, 0)
    End Function

    Public Shared Function CreateDefaultMetaData() As XmlMetaData
        Dim doc As XDocument = CreateMetaDataDocument()
        Return New XmlMetaData(doc)
    End Function

End Class

Public Class AggAssocTest
    Inherits AggregateAssociationTask

    Private _counter As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Return _counter
        End Get
    End Property

    Public Overrides Sub Execute(ByVal metaData As XmlMetaData, ByVal context As AppContext)
        _counter = 0
        MyBase.Execute(metaData, context)
    End Sub

    Protected Overrides Sub ExecuteCore(ByVal item As MetaDataAssociation, ByVal context As AppContext)
        MyBase.ExecuteCore(item, context)
        _counter += 1
    End Sub

End Class

Public Class AggColTest
    Inherits AggregateColumnTask

    Private _counter As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Return _counter
        End Get
    End Property

    Public Overrides Sub Execute(ByVal metaData As XmlMetaData, ByVal context As AppContext)
        _counter = 0
        MyBase.Execute(metaData, context)
    End Sub

    Protected Overrides Sub ExecuteCore(ByVal item As MetaDataColumn, ByVal context As AppContext)
        MyBase.ExecuteCore(item, context)
        _counter += 1
    End Sub

End Class

Public Class AggTableTest
    Inherits AggregateTableTask

    Private _counter As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Return _counter
        End Get
    End Property

    Public Overrides Sub Execute(ByVal metaData As XmlMetaData, ByVal context As AppContext)
        _counter = 0
        MyBase.Execute(metaData, context)
    End Sub

    Protected Overrides Sub ExecuteCore(ByVal item As MetaDataTable, ByVal context As AppContext)
        MyBase.ExecuteCore(item, context)
        _counter += 1
    End Sub

End Class

Public Class AggTypeTest
    Inherits AggregateTypeTask

    Private _counter As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Return _counter
        End Get
    End Property

    Public Overrides Sub Execute(ByVal metaData As XmlMetaData, ByVal context As AppContext)
        _counter = 0
        MyBase.Execute(metaData, context)
    End Sub

    Protected Overrides Sub ExecuteCore(ByVal item As MetaDataType, ByVal context As AppContext)
        MyBase.ExecuteCore(item, context)
        _counter += 1
    End Sub

End Class
